package com.zcyr.core.impl;

import com.zcyr.core.AbstractDatabaseEnvironment;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(value = "tryme.databaseEnvironmentManager.enable_pool", havingValue = "true")
public class PooledDatabaseEnvironmentFactory extends BasePooledObjectFactory<AbstractDatabaseEnvironment> {
    @Autowired
    PooledDatabaseEnvironmentManager manager;

    @Override
    public AbstractDatabaseEnvironment create() throws Exception {
        return manager.generateDatabaseEnvironment();
    }

    @Override
    public PooledObject<AbstractDatabaseEnvironment> wrap(AbstractDatabaseEnvironment databaseEnvironment) {
        return new DefaultPooledObject<>(databaseEnvironment);
    }
}
